﻿using System;
using System.Globalization;
using System.Web.Security;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web.Employees
{
    public partial class Edit : System.Web.UI.Page
    {
        #region Properties

        private EmployeeBiz _employeeBiz;

        private EmployeeBiz EmployeeManager
        {
            get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); }
        }

        private int _employeeId;
        private int EmployeeId
        {
            get
            {
                if (_employeeId <= 0)
                    int.TryParse(Request.QueryString["Id"], out _employeeId);
                return _employeeId;
            }
        }

        private Employee _employee;
        private Employee CurrentEmployee
        {
            get
            {
                if (_employee == null && EmployeeId > 0)
                {
                    _employee = EmployeeManager.GetById(EmployeeId);
                }
                return _employee;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load data
                if (CurrentEmployee == null)
                {
                    lblErrorMessage.Text = string.Format("{0}: Id={1}", Resources.GlobalStrings.NotExisted, EmployeeId);
                    btnSave.Enabled = false;
                }
                else
                {
                    txtCode.Text = CurrentEmployee.Code;
                    txtName.Text = CurrentEmployee.Name;
                    txtUserName.Text = CurrentEmployee.UserName;
                    txtPassword.Text = CurrentEmployee.Password;
                    txtEmailAddress.Text = CurrentEmployee.MailAddress;
                    txtPhoneNumber.Text = CurrentEmployee.PhoneNumber;
                    txtDescription.Text = CurrentEmployee.Description;
                    txtSortOrder.Text = CurrentEmployee.SortOrder.ToString(CultureInfo.InvariantCulture);
                    chkHidden.Checked = CurrentEmployee.Hidden;
                    chkAdmin.Checked = CurrentEmployee.IsAdmin;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.CodeIsRequired;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.NameIsRequired;
                return;
            }
            var userName = txtUserName.Text.Trim();
            var email = txtEmailAddress.Text.Trim();
            var phone = txtPhoneNumber.Text.Trim();
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;
            var isAdmin = chkAdmin.Checked;

            CurrentEmployee.Code = code;
            CurrentEmployee.Name = name;
            CurrentEmployee.UserName = userName;
            CurrentEmployee.Password = txtPassword.Text;
            CurrentEmployee.MailAddress = email;
            CurrentEmployee.PhoneNumber = phone;
            CurrentEmployee.Description = descrition;
            if (txtPassword.Text.Length > 0)
            {
                CurrentEmployee.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "SHA1");
            }
            if (sortOrder > 0)
                CurrentEmployee.SortOrder = sortOrder;
            CurrentEmployee.Hidden = isHidden;
            CurrentEmployee.IsAdmin = isAdmin;

            // update
            try
            {
                EmployeeManager.UpdateEmployee(CurrentEmployee);
            }
            catch (ApplicationException appEx)
            {
                lblErrorMessage.Text = WarrantyCommon.GetGlobalString(appEx.Message);
                return;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            Response.Redirect("List.aspx");
        }

        #endregion
    }
}